<?php
session_start();
?>

<?php include('./classes/mailer.php'); ?>
<?php include('./classes/employee.php'); ?>
<?php 
require_once('KLogger.php');
$log = new KLogger ( "log.txt" , KLogger::DEBUG );
?>


<?php
//Creating an array 
$id = $_SESSION['id'];
$external_idce_reqd = $_SESSION['external_idce_reqd'];
$i=0;

//fetch the REQUEST variable data..
$serial = array();
$choice = array();
$comments = array();


for ($i=1; $i<=58 ; $i++) {
$serial[] = $i;
$choice[] = $_REQUEST['select'."".$i];
$comments[] = $_REQUEST['designer_comments'."".$i];
}


$log->logInfo('processChecklist[17] Initial logging : '.$_SESSION['id'].'ok');

//Checking if the Checklist for this CR is already filled..
$con = mysql_connect("localhost","root","");
mysql_select_db("checklist", $con);
$check = "select * from checklist where id = '$id' ";
$result = mysql_query($check);

//Check if the Save button was clicked and there was already checklist....
if(mysql_num_rows($result) && isset($_REQUEST['save']))
{
for ($i=0; $i<=57 ; $i++)
{
$sql = "update checklist set choice='$choice[$i]' , designer_comments='$comments[$i]' where id='$id' AND serial=($i+1) ";
mysql_query($sql,$con) or die('Error: ' . mysql_error());
}
echo '<script type="text/javascript">','alert("Data fed Successfully !!!");',
'window.location = "http://172.26.195.159/checklist/php/mainpage.php";' ,'</script>';

}

//Check if the Save button was clicked and but there was no data in checklist..
if(!mysql_num_rows($result) && isset($_REQUEST['save']))
{
for ($i=0; $i<=57 ; $i++)
{
$sql = "insert into checklist(serial ,id , choice , designer_comments) values('$serial[$i]' ,'$id','$choice[$i]' , '$comments[$i]')";
mysql_query($sql,$con) or die('Error: ' . mysql_error());
}
echo '<script type="text/javascript">','alert("Data Saved Successfully !!!");',
'window.location = "http://172.26.195.159/checklist/php/mainpage.php";' ,'</script>';

}

if(!mysql_num_rows($result) && isset($_REQUEST['submit']))
{
echo '<script type="text/javascript">','alert("Sorry Checklist has not been filled yet !!!");',
'window.location = "http://172.26.195.159/checklist/php/mainpage.php";' ,'</script>';
}



if(isset($_REQUEST['submit']) && mysql_num_rows($result))
{
//Simply update status in case the details already filled and send the mail..
$s = "update cr_status set status=1 where id = '$id'";
mysql_query($s,$con) or die('Error: ' . mysql_error());

//*********************************Send Mail***************************************************

$m = new mailer();

//Gathering data....
$sql = "select * from cr where id = '$id' ";
$result = mysql_query($sql);
$data_from_cr_for_mail = mysql_fetch_array($result);

//Feeding Data for CR..
$m->set_id($id);
$m->set_title($data_from_cr_for_mail['title']);
$m->set_status(1);

//Fetching designer's mailing address details....
$query = "SELECT email FROM employees WHERE username in(SELECT designer FROM cr where id = '$id')"; 
$result = mysql_query($query);
$mail_designer_data = mysql_fetch_array($result); 

$mail_designer = $mail_designer_data['email'];  

$log->logInfo('Designer email address : '.$mail_designer.'ok');
                     //got the mail of designer...
mysql_free_result($result);                                         //freeing the result

//fetching usernames.
$query = "select checklist_reviewer,internal_idce_reviewer,external_idce_reviewer,pl,dp_prime from cr_status where id='$id'";
$result = mysql_query($query);
$reviewers_names = mysql_fetch_array($result);                       
$checklist_reviewer = $reviewers_names['checklist_reviewer'];
$internal_idce_reviewer = $reviewers_names['internal_idce_reviewer'];
$external_idce_reviewer = $reviewers_names['external_idce_reviewer'];
$dp_prime = $reviewers_names['dp_prime'];
$pl = $reviewers_names['pl'];

//fetching mail_addresses..
$query = "select email from employees where username = '$checklist_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);  
$mail_checklist_reviewer = $mail_reviewers['email'];

$query = "select email from employees where username = '$external_idce_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_external_idce_reviewer = $mail_reviewers['email'];

$query = "select email from employees where username = '$pl' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_pl = $mail_reviewers['email'];

$query = "select email from employees where username = '$dp_prime' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_dp_prime = $mail_reviewers['email'];

$query = "select email from employees where username = '$internal_idce_reviewer' ";
$result = mysql_query($query);
$mail_reviewers = mysql_fetch_array($result);   
$mail_internal_idce_reviewer = $mail_reviewers['email'];

// '$checklist_reviewer' , '$internal_idce_reviewer','$external_idce_reviewer','$pl','$dp_prime'

$cc = array();
$cc[0]=$mail_designer;
$cc[1]=$mail_internal_idce_reviewer;
$cc[2]=$mail_external_idce_reviewer ;
$cc[3]=$mail_pl;
$cc[4]=$mail_dp_prime;


$log->logInfo('processChecklist[152]  $mail_designer  : '.$mail_designer);
$log->logInfo('processChecklist[152]  $mail_checklist_reviewer :'.$mail_checklist_reviewer);

//Setting to and ccs'
$m->set_to($mail_checklist_reviewer);

//Setting message and body..
$m->set_headers('From: "WebChecklist Notification" <WebChecklist_Message_Broker@tcs.com>' . "\r\n" .
"CC: ".$cc[0]." , ".$cc[1]." , ".$cc[2]." , ".$cc[3]." , ".$cc[4]);


$m->set_subject("Status of CR : ".$id." has been changed to ".getStatus(1)."");
$m->set_body("Hi , "."\n\n"."The Checklist Review of CR : ".$id." is pending against you ."."Please take appropriate action"."\n\n\n"."Regards ,"."\n"."iVSE - WebChecklist Messenger"
."\n\n"."In case of concerns :"."\n"."mailto:gaurav.pruthi@tcs.com"."\n"."ESN : 3048");


//Sending mail...
$m->sendmail();	
//*******************************End Mail Part**********************************************

echo '<script type="text/javascript">','alert("Checklist Successfully Submitted!!!");',
'window.location = "http://172.26.195.159/checklist/php/mainpage.php";' ,'</script>';

}

?>



